package com.trimps.wifi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trimps.wifi.pojo.FlowData;
import com.trimps.wifi.pojo.MultiQueryData;
import com.trimps.wifi.service.MultiQueryDataService;

import net.sf.json.*;

@Controller
@RequestMapping(value = "data/")
public class MultiQueryDataController extends BaseController {
    private static final Logger logger = Logger.getLogger(MultiQueryDataController.class);

    @Autowired
    private MultiQueryDataService multiQueryDataService;

    /**
     * 获取列表
     * @param page  这里必须不能写@RequstParam注解，否则不能正常映射
     * @param map
     * @return
     */
    @RequestMapping(value = "multiQuery")
    public String multiQueryPage() {
        return "data/multiQuery";
    }
    
    /**
     * 获取列表信息
     * @param 
     * @return
     */
    @RequestMapping(value = "multiQueryList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getListInfo(@RequestParam("filterData") String filterData)  {
        JSONArray jsonArray = JSONArray.fromObject(filterData);

        String sqlStr = "";
        List<Map<String,Object>> filtersData = (List)jsonArray;  
        for (int i = 0; i < filtersData.size(); i++) {  
            Map<String,Object> obj=filtersData.get(i);  
            sqlStr += " select distinct mac from data_flow where dev_no = '"+obj.get("devices")+"' and in_time >= '"+obj.get("startTime")+"' and go_time <= '"+obj.get("endTime")+"'";
            sqlStr += " union all ";
        }  
        sqlStr = sqlStr.substring(0,sqlStr.length()-10);
        int num = filtersData.size();
        // 返回用
        Map<String, Object> map = new HashMap<String, Object>();
        List<MultiQueryData> lst = new ArrayList<MultiQueryData>();
        lst = multiQueryDataService.getMultiQueryList(sqlStr,num);       
        map.put("listInfo", lst);
        return map;
    }
    
    /**
     * 获取mac列表信息
     * @param 
     * @return
     */
    @RequestMapping(value = "macInfoList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getMacListInfo(@RequestParam("filterData") String filterData,@RequestParam("mac") String mac)  {
        JSONArray jsonArray = JSONArray.fromObject(filterData);

        String sqlStr = "";
        List<Map<String,Object>> filtersData = (List)jsonArray;  
        for (int i = 0; i < filtersData.size(); i++) {  
            Map<String,Object> obj=filtersData.get(i);  
            sqlStr += " (dev_no = '"+obj.get("devices")+"' and in_time >= '"+obj.get("startTime")+"' and go_time <= '"+obj.get("endTime")+"')";
            sqlStr += " or ";
        }  
        sqlStr = sqlStr.substring(0,sqlStr.length()-3);
        // 返回用
        Map<String, Object> map = new HashMap<String, Object>();
        List<FlowData> lst = new ArrayList<FlowData>();
        lst = multiQueryDataService.getMacListInfo(sqlStr,mac);       
        map.put("listInfo", lst);
        return map;
    }
    
}
